<template>
  <a-modal
    title="编辑"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item v-show="false" label="主键ID">
          <a-input v-decorator="['id', { initialValue: model?.id }]" disabled />
        </a-form-item>
        <a-form-item label="销售员">
          <a-select v-decorator="['empId', { initialValue: model?.empId }]" placeholder="请选择">
            <a-select-option :value="i.id" v-for="i in userOption" :key="i.id">{{ i.userName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="销售日期">
          <a-date-picker
            v-decorator="['orderDate', { initialValue: model?.orderDate }]"
            style="width: 100%"
            valueFormat="YYYY-MM-DD"
          />
        </a-form-item>
        <a-form-item label="备注">
          <a-textarea rows="2" v-decorator="['memo', { initialValue: model?.memo }]" placeholder="请输入备注"/>
        </a-form-item>
        <a-form-item
          label="选择会员"
          :labelCol="{ lg: { span: 7 }, sm: { span: 7 } }"
          :wrapperCol="{ lg: { span: 10 }, sm: { span: 17 } }"
        >
          <a-space >
            <a-input-search
              v-model="value"
              placeholder="请输入手机号或者会员卡号"
              enter-button
              @change="handleChange"
              @search="onSearch"
            />
            <a-input v-show="false" v-decorator="['memId', { initialValue: model?.memId }]"/>
            <a-button type="primary" v-if="JSON.stringify(this.memberDetail) !== '{}'" @click="cancel">取消</a-button>
          </a-space>
        </a-form-item>
        <a-form-item
          label="会员详情"
          v-if="JSON.stringify(this.memberDetail) !== '{}'"
          :labelCol="{ lg: { span: 7 }, sm: { span: 7 } }"
          :wrapperCol="{ lg: { span: 10 }, sm: { span: 17 } }"
        >
          <a-descriptions>
            <a-descriptions-item label="会员名称" :span="3">{{ memberDetail.name }}</a-descriptions-item>
            <a-descriptions-item label="会员电话" :span="3">{{ memberDetail.phone }}</a-descriptions-item>
            <a-descriptions-item label="会员卡号" :span="3">{{ memberDetail.cardNo }}</a-descriptions-item>
            <a-descriptions-item label="会员积分" :span="3">{{ memberDetail.hisBigintegral }}</a-descriptions-item>
          </a-descriptions>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>
  
  <script>
import pick from 'lodash.pick'
import { getEmployeesListUsingPOST } from '@/api/api/yuangongguanli.ts'
import {getMemberByNumberUsingGET} from '@/api/api/huiyuanguanli.ts'
// 表单字段
const fields = ['empId', 'orderDate','memo','memId','id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
    }
    return {
      optionParam: {
        pageNo: 1,
        pageSize: 999,
        queryCondition: '',
        status: 1,
      },
      memberDetail:{},
      value:'',
      userOption: [],
      form: this.$form.createForm(this),
    }
  },
  created() {
    // 防止表单未注册
    this.getUserOption()
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.memberDetail.name = this.model.memName
      this.memberDetail.phone = this.model.memPhone
      this.memberDetail.cardNo = this.model.cardNo
      this.value = this.model.memPhone ?? this.model.cardNo
      this.memberDetail.hisBigintegral = this.model.bigintegral
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    cancel(){
      this.memberDetail = {}
      this.form.setFieldsValue({memId:''})
    },
    onSearch(value) {
      if(!value){
        this.$message.warn('请输入正确的手机号或会员卡号')
      }
      this.handleMember(value)
    },
    handleChange(e) {
      this.value = e.target.value
    },
    handleMember(params) {
      getMemberByNumberUsingGET({
        number: params,
      })
        .then((res) => {
          if(res.data){
            this.memberDetail = res.data
            this.form.setFieldsValue({memId:res.data.id})
          }else{
            this.memberDetail = {}
          }
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },

    getUserOption() {
      getEmployeesListUsingPOST(this.optionParam)
        .then((res) => {
          this.userOption = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
  },
}
</script>